﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows.Forms;
using QuanLyKhachSan.report;
using QuanLyKhachSan.DanhMuc;

namespace QuanLyKhachSan
{
    public partial class frmInHoaDon : DevExpress.XtraEditors.XtraForm
    {
        public frmInHoaDon()
        {
            InitializeComponent();
        }
        public int cttp_id, tp_id, kh_id = 0, phuthu;
        public Decimal tiendichvu, thucan, tongtien;
        public DateTime tu = DateTime .Parse ( "01/01/1900"), den = DateTime .Parse ( "31/12/2100");
        DataClassesKhachSanDataContext db = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString);


        ParameterField paraField;
        ParameterFields paraFields = new ParameterFields();
        ParameterDiscreteValue paraDiscreteValue;
        public void PassParameter(string parameterFieldName, string parameterDiscreteValue, CrystalReportViewer viewer)
        {
            paraField = new ParameterField();
            paraField.Name = parameterFieldName;//tên c?a parameter
            paraDiscreteValue = new ParameterDiscreteValue();
            paraDiscreteValue.Value = parameterDiscreteValue;//gán giá tr? cho parameter
            paraField.CurrentValues.Add(paraDiscreteValue);
            paraFields.Add(paraField);
            viewer.ParameterFieldInfo = paraFields;
        }

        private void frmInHoaDon_Load(object sender, EventArgs e)
        {
            this.fillData();
        }
        void fillData()
        {
            //In hoa don cho 1 phong
                DataTable dtChiTiet = new DataTable();
                dtChiTiet.Columns.Add("KH_CMND");
                dtChiTiet.Columns.Add("KH_HOTEN");
                dtChiTiet.Columns.Add("KH_DIACHI");
                dtChiTiet.Columns.Add("KH_DIENTHOAI");
                dtChiTiet.Columns.Add("TP_ID");
                dtChiTiet.Columns.Add("TP_LOAI_GIA");
                dtChiTiet.Columns.Add("TP_TONGCONG", System.Type.GetType("System.Double"));
                dtChiTiet.Columns.Add("CTTP_ID");
                dtChiTiet.Columns.Add("P_ID");
                dtChiTiet.Columns.Add("CTTP_NGAYBD", System.Type.GetType("System.DateTime"));
                dtChiTiet.Columns.Add("CTTP_NGAYKT", System.Type.GetType("System.DateTime"));
                dtChiTiet.Columns.Add("CTTP_DONGIA", System.Type.GetType("System.Double"));
                dtChiTiet.Columns.Add("P_MA");
                dtChiTiet.Columns.Add("PHU_THU", System.Type.GetType("System.Int16"));


                DataTable dtDichVu = new DataTable();
                dtDichVu.Columns.Add("TP_ID");
                dtDichVu.Columns.Add("P_ID");
                dtDichVu.Columns.Add("SD_THOIDIEM");
                dtDichVu.Columns.Add("SD_SL", System.Type.GetType("System.Int16"));
                dtDichVu.Columns.Add("SD_DONGIA", System.Type.GetType("System.Double"));
                dtDichVu.Columns.Add("DV_TEN");
                dtDichVu.Columns.Add("DV_DONVITINH");


                DataTable dtThucAn = new DataTable();
                dtThucAn.Columns.Add("GTAN_ID");
                dtThucAn.Columns.Add("CTTP_ID");
                dtThucAn.Columns.Add("G_THOIDIEM");
                dtThucAn.Columns.Add("G_SL", System.Type.GetType("System.Int16"));
                dtThucAn.Columns.Add("G_DONGIA", System.Type.GetType("System.Double"));
                dtThucAn.Columns.Add("P_ID");
                dtThucAn.Columns.Add("DT_ID");
                dtThucAn.Columns.Add("DT_TEN");
                dtThucAn.Columns.Add("DT_DVI");


                rptHoaDonPhong reportXepHang = new rptHoaDonPhong();
                //reportXepHang.Load("report/rptHoaDonPhong.rpt");
                //lay gia tri dich vu
                var dichvu = (from dv in db.SU_DUNGs
                              where dv.SD_CHITIETTPID == cttp_id
                              select new
                              {
                                  dv.CHI_TIET_THUE_PHONG.TP_ID,
                                  dv.CHI_TIET_THUE_PHONG.P_ID,
                                  dv.SD_DICHVUID,
                                  dv.SD_THOIDIEM,
                                  dv.SD_DONGIA,
                                  dv.SD_SL,
                                  dv.DICH_VU.DV_DONVITINH,
                                  dv.DICH_VU.DV_TEN,
                                  THANHTIEN = dv.SD_SL * dv.SD_DONGIA,

                              }).ToList();

                //lay gia tri thuc an
                var monan = (from m in db.GOI_THUC_AN_NHANHs
                             where m.CTTP_ID == cttp_id
                             select new
                             {
                                 m.GTAN_ID,
                                 m.CTTP_ID,
                                 m.DT_ID,
                                 m.G_THOIDIEM,
                                 m.G_SL,
                                 m.G_DONGIA,
                                 m.CHI_TIET_THUE_PHONG.P_ID,
                                 m.THUC_UONG_THUC_AN_NHANH.DOI_TUONG.DT_DVI,
                                 m.THUC_UONG_THUC_AN_NHANH.DOI_TUONG.DT_TEN,
                                 THANHTIEN = m.G_SL * m.G_DONGIA
                             }).ToList();

                        var chitiet = (from c in db.CHI_TIET_THUE_PHONGs
                                       where c.CTTP_ID == cttp_id 
                                       select new
                                       {
                                           c.TP_ID,
                                           c.THUE_PHONG.KH_ID,
                                           c.THUE_PHONG.KHACH_HANG.KH_HOTEN,
                                           c.THUE_PHONG.KHACH_HANG.KH_CMND,
                                           c.THUE_PHONG.KHACH_HANG.KH_DIENTHOAI,
                                           c.THUE_PHONG.KHACH_HANG.KH_DIACHI,
                                           c.THUE_PHONG.TP_LOAI_GIA,
                                           c.THUE_PHONG.TP_TONGCONG,
                                           c.CTTP_ID,
                                           c.P_ID,
                                           c.CTTP_NGAYBD,
                                           c.CTTP_NGAYKT,
                                           c.CTTP_DONGIA,
                                           c.PHONG.P_MA

                                       }).ToList();

                        for (int i = 0; i < chitiet.Count; i++)
                        {
                            string dongia;
                            if (chitiet[i].TP_LOAI_GIA == true)
                            {
                                dongia = "Ngày";

                            }
                            else
                            {
                                dongia = "Giờ";
                            }

                            dtChiTiet.Rows.Add(new object[]
                          {
                              chitiet[i].KH_CMND,
                              chitiet[i].KH_HOTEN,
                              chitiet[i].KH_DIACHI,
                              chitiet[i].KH_DIENTHOAI,
                              chitiet[i].TP_ID ,
                              dongia ,
                              chitiet[i].TP_TONGCONG,
                              chitiet[i].CTTP_ID ,
                              chitiet[i].P_ID,
                              chitiet[i].CTTP_NGAYBD,
                              chitiet[i].CTTP_NGAYKT,
                              chitiet[i].CTTP_DONGIA,
                              chitiet[i].P_MA,
                              phuthu 
              
                });

                        }




                for (int i = 0; i < dichvu.Count; i++)
                {
                    dtDichVu.Rows.Add(new object[]
                          {
                             dichvu[i].TP_ID,
                             dichvu[i].P_ID,
                             dichvu[i].SD_THOIDIEM,
                             dichvu[i].SD_SL,
                             dichvu[i].SD_DONGIA,
                             dichvu[i].DV_TEN,
                             dichvu[i].DV_DONVITINH

                          });
                }

                for (int i = 0; i < monan.Count; i++)
                {
                    dtThucAn.Rows.Add(new object[]
                          {
                             monan[i].GTAN_ID,
                             monan[i].CTTP_ID,
                             monan[i].G_THOIDIEM,
                             monan[i].G_SL,
                             monan[i].G_DONGIA,
                             monan[i].P_ID,
                             monan[i].DT_ID,
                             monan[i].DT_TEN,
                             monan[i].DT_DVI
                          });
                }
                DataSet ds = new DataSet();

                // dtChiTiet.TableName("HOADONPHONG");
                dtChiTiet.TableName = "HOADONPHONG";
                dtDichVu.TableName = "DICHVU";
                dtThucAn.TableName = "THUCAN";


                ds.Tables.Add(dtChiTiet);
                ds.Tables.Add(dtThucAn);
                ds.Tables.Add(dtDichVu);

                reportXepHang.SetDataSource(ds);
                crystalReportViewer1.ReportSource = reportXepHang;

                this.PassParameter("ThucAn", thucan.ToString(), crystalReportViewer1);
                this.PassParameter("DichVu", tiendichvu.ToString(), crystalReportViewer1);
                this.PassParameter("TongTien", tongtien.ToString(), crystalReportViewer1);
            }
        //}
    }
}